<% availableModels ||= available_models %>


<script>
  var pageless_state = {
    "excursions":{
      "url": "<%=user_url(profile_subject)%>/excursions",
      "num_pages":0,
      "current_page":0
    },
    "workshops":{
      "url": "<%=user_url(profile_subject)%>/workshops",
      "num_pages":0,
      "current_page":0
    },
    "resources":{
      "url": "<%= user_url(profile_subject) %>/resources",
      "num_pages":0,
      "current_page":0
    },
    "categories":{
      "url": "<%= user_url(profile_subject) %>/categories",
      "num_pages":0,
      "current_page":0
    },
    "events":{
      "url": "<%= user_url(profile_subject) %>/events",
      "num_pages":0,
      "current_page":0
    },
    "followers":{
      "url": "<%= user_url(profile_subject) %>/followers",
      "num_pages":0,
      "current_page":0
    },
    "followings":{
      "url": "<%= user_url(profile_subject) %>/followings",
      "num_pages":0,
      "current_page":0
    }
  };

  function update_pageless_state(tab, num_pages, current_page){
    pageless_state[tab].num_pages = num_pages;
    pageless_state[tab].current_page = current_page;
  }

  function re_apply_pageless(tab){
    $.pagelessStop(); //remove old pageless
    $("#" + tab + "_tab_ready ul.items").pageless({
      totalPages: pageless_state[tab].num_pages,
      url: get_url_with_sort_by(tab),
      currentPage: pageless_state[tab].current_page,
      loader: '.loader_pagination',
      end: function(){
        $('.loader_pagination').hide();
      }
    });
  }

  function get_url_with_sort_by(tab){
    var the_url = pageless_state[tab].url;
    if (tab != "categories"){
      the_url = the_url + "?sort_by=" + $("#order_by_selector_" + tab).attr("li_selected");
    }
    return the_url;
  }

</script>

<%content_for :javascript do%>

  var excursions_tab_loaded = false;
  var workshops_tab_loaded = false;
  var resources_tab_loaded = false;
  var events_tab_loaded = false;
  var categories_tab_loaded = false;
  var followers_tab_loaded = false;
  var followings_tab_loaded = false;

    $("#excursions_tab").click(function(){
      updateWindowTopUrl($(this).attr("id"));
      if(excursions_tab_loaded === false){
        $('.loader_pagination').show();
        excursions_tab_loaded = true;
        $.ajax({
          type : "GET",
          url : "<%=user_url(profile_subject)%>/excursions",
          success : function(html) {
            $('.loader_pagination').hide();
            $("#excursions_tab_ready").append(html);
            re_apply_pageless("excursions");
          },
          error: function(){
          }
        });
      } else{
        re_apply_pageless("excursions");
      }
    });

    $("#workshops_tab").click(function(){
      updateWindowTopUrl($(this).attr("id"));
      if(workshops_tab_loaded===false){
        $('.loader_pagination').show();
        workshops_tab_loaded = true;
        $.ajax({
          type : "GET",
          url : "<%=user_url(profile_subject)%>/workshops",
          success : function(html) {
            $('.loader_pagination').hide();
            $("#workshops_tab_ready").append(html);
            re_apply_pageless("workshops");
          },
          error: function(){
         }
        });
      } else {
        re_apply_pageless("workshops");
      }
    });

    $("#resources_tab").click(function(){
      updateWindowTopUrl($(this).attr("id"));
      if(resources_tab_loaded===false){
        $('.loader_pagination').show();
        resources_tab_loaded = true;
        $.ajax({
          type : "GET",
          url : "<%=user_url(profile_subject)%>/resources",
          success : function(html) {
            $('.loader_pagination').hide();
            $("#resources_tab_ready").append(html);
            re_apply_pageless("resources");
          },
          error: function(){
         }
        });
      } else {
        re_apply_pageless("resources");
      }
    });

    $("#events_tab").click(function(){
      updateWindowTopUrl($(this).attr("id"));
      if(events_tab_loaded===false){
        $('.loader_pagination').show();
        events_tab_loaded = true;
        $.ajax({
          type : "GET",
          url : "<%=user_url(profile_subject)%>/events",
          success : function(html){
            $('.loader_pagination').hide();
            $("#events_tab_ready").append(html);
            re_apply_pageless("events");
          },
          error: function(){
         }
        });
      } else {
        re_apply_pageless("events");
      }
    });

    $("#categories_tab").click(function(){
      updateWindowTopUrl($(this).attr("id"));
      if (categories_tab_loaded === false){
        $('.loader_pagination').show();
        categories_tab_loaded = true;
        $.ajax({
          type : "GET",
          url : "<%=user_url(profile_subject)%>/categories",
          success : function(html) {
            $('.loader_pagination').hide();
            $("#categories_tab_ready").append(html);
            re_apply_pageless("categories");
          },
          error: function(){
         }
        });
      } else {
        re_apply_pageless("categories");
      }
    });

    $("#followers_tab").click(function(){
      updateWindowTopUrl($(this).attr("id"));
      if(followers_tab_loaded===false){
        $('.loader_pagination').show();
        followers_tab_loaded = true;
        $.ajax({
          type : "GET",
          url : "<%=user_url(profile_subject)%>/followers",
          success : function(html) {
            $('.loader_pagination').hide();
            $("#followers_ul").prepend(html);
            re_apply_pageless("followers");
          },
          error: function(){
         }
        });
      } else {
        re_apply_pageless("followers");
      }
    });

    $("#followings_tab").click(function(){
      updateWindowTopUrl($(this).attr("id"));
      if(followings_tab_loaded===false){
        $('.loader_pagination').show();
        followings_tab_loaded = true;
        $.ajax({
          type : "GET",
          url : "<%= user_url(profile_subject) %>/followings",
          success : function(html) {
            $('.loader_pagination').hide();
            $("#followings_ul").prepend(html);
            re_apply_pageless("followings");
          },
          error: function(){
         }
        });
      } else {
        re_apply_pageless("followings");
      }
    });


  var canUseHistoryPushState = ((typeof history === "object")&&(typeof history.pushState == "function"));
  function updateWindowTopUrl(tabName){
    if(canUseHistoryPushState){
      var newURL = '<%=profile_subject.slug%>' + "?tab=" + tabName.replace("_tab","");
      window.history.pushState(undefined, undefined, newURL);
    }
  };

<%end%>


<section class="info-profile">

    <ul class="nav nav-tabs">

      <% hasParamsTab = params[:tab].nil? %>
      <% params[:tab] ||= availableModels.first.downcase.pluralize %>

<!-- dropdown menu links -->
      <% availableModels.each do |modelName| %>
        <% if modelName == "Excursion" %>
          <!--excursions-->
          <li class="<%= params[:tab]=='excursions' ? 'active':''%>">
            <a id="excursions_tab" href="#tabExcursions" data-toggle="tab">
              <i class="fa fa-list-alt"></i>
              <span class=" hidden-xs"><%=t('excursion.other')%></span>
            </a>
          </li>
        <% elsif modelName == "Workshop" %>
          <li class="<%= params[:tab]=='workshops' ? 'active':''%>">
            <a id="workshops_tab" href="#tabWorkshops" data-toggle="tab">
              <i class="fa fa-book"></i>
              <span class=" hidden-xs"><%=t('workshop.other')%></span>
            </a>
          </li>
        <% elsif modelName == "Resource" %>
          <!--resources-->
          <li class="<%= params[:tab]=='resources' ? 'active':''%>">
            <a id="resources_tab" href="#tabResources" data-toggle="tab">
              <i class="fa fa-file"></i>
              <span class=" hidden-xs"><%=t('resource.other')%></span>
            </a>
          </li>
        <% elsif modelName == "Category" %>
          <li class="<%= params[:tab]=='categories' ? 'active':''%>">
            <a id="categories_tab" href="#tabCategories" data-toggle="tab">
              <i class="fa fa-th-large"></i>
              <span class=" hidden-xs"><%=t('categories.other')%></span>
            </a>
          </li>
        <% elsif modelName == "Event" %>
          <!--events-->
          <li class="<%= params[:tab]=='events' ? 'active':''%>">
            <a id="events_tab" href="#tabEvents" data-toggle="tab">
              <i class="fa fa-video-camera"></i>
              <span class=" hidden-xs"><%=t('event.other')%></span>
            </a>
          </li>
        <%end%>
      <%end%>


      <!--followers-->
      <li class="follow-tab <%= params[:tab]=='followers' ? 'active':''%>">
        <a id="followers_tab" href="#tab5" data-toggle="tab">
          <span><strong><%= profile_subject.followers.count.to_s %></strong></span>
          <!--<i class="fa fa-group fa-fw"></i>-->
          <span class="hidden-xs"><%= t('follow.followers') %></span>
        </a>
      </li>

      <!--followings-->
      <li class="follow-tab <%= params[:tab]=='followings' ? 'active':''%>">
        <a id="followings_tab" href="#tab6" data-toggle="tab">
          <span><strong><%= profile_subject.following_actor_objects.includes(:actor).count.to_s %></strong></span>
          <!--<i class="fa fa-group fa-fw"></i>-->
          <span class="hidden-xs"><%= t('follow.followings') %></span>
        </a>
      </li>
    </ul>


  <!-- **************** -->
    <!-- TAB CONTENT -->
  <!-- **************** -->

    <div class="tab-content">

      <!-- EXCURSIONS -->

      <% if availableModels.include? "Excursion" %>
        <div class="tab-pane <%=params[:tab]=='excursions' ? 'active':''%>" id="tabExcursions">

          <%= render partial: 'users/order_by_dropdown_excursions' %>
          <div id="excursions_tab_ready"  class="entities excursions">
            <% if params[:tab]== 'excursions' %>
              <% default_sort_by = profile_subject_is_current? ? "updated_at" : "popularity"%>
              <%= render partial: 'excursions/profile_excursions_list', :locals => {:scope => :me, :limit => 0, :page=> params[:page] || 1, :sort_by=> params[:sort_by]||default_sort_by}%>
            <%end%>
          </div>

          <div class="loader_pagination" style="display:none">
            <div class="pageless-loader" style="text-align:center;width:100%;">
              <div class="msg" style="font-size:2em">
                <%=t('search.loading')%>
              </div>
              <%= render :partial => "layouts/loading" %>
            </div>
          </div>

        </div>
      <%end%>

      <!-- RESOURCES -->

      <% if availableModels.include? "Resource" %>
        <div class="tab-pane <%=params[:tab]=='resources' ? 'active':''%>" id="tabResources">

          <%= render partial: 'users/order_by_dropdown_resources' %>
          <div id="resources_tab_ready" class="resources">
            <% if params[:tab] == 'resources'%>
              <% default_sort_by = profile_subject_is_current? ? "updated_at" : "popularity"%>
              <%= render partial: 'repositories/profile_resources_list', :locals => {:scope => :me, :limit => 0, :page=> params[:page] || 1, :sort_by=> params[:sort_by]||default_sort_by}%>
            <%end%>
          </div>

          <div class="loader_pagination" style="display:none">
            <div class="pageless-loader" style="text-align:center;width:100%;">
              <div class="msg" style="font-size:2em">
                <%=t('search.loading')%>
              </div>
              <%= render :partial => "layouts/loading" %>
            </div>
          </div>

        </div>
      <%end%>

      <!-- CATEGORIES -->

      <% if availableModels.include? "Category" %>
        <div class="tab-pane <%=params[:tab]=='categories' ? 'active':''%>" id="tabCategories">

          <div id="categories_tab_ready" class="categories cat_container_generic">
            <%if params[:tab] == 'categories'%>
              <% default_sort_by = profile_subject_is_current? ? "updated_at" : "popularity"%>
              <%= render partial: 'categories/profile_categories_list', :locals => {:scope => :me, :limit => 0, :page=> params[:page] || 1, :sort_by=> params[:sort_by]||default_sort_by}%>
            <%end%>
          </div>

          <div class="loader_pagination" style="display:none">
            <div class="pageless-loader" style="text-align:center;width:100%;">
              <div class="msg" style="font-size:2em">
                <%=t('search.loading')%>
              </div>
              <%= render :partial => "layouts/loading" %>
            </div>
          </div>

        </div>
      <%end%>

      <!-- EVENTS -->

      <% if availableModels.include? "Event" %>
        <div class="tab-pane <%= params[:tab]== 'events' ? 'active':''%>" id="tabEvents">

          <%= render partial: 'users/order_by_dropdown_events' %>
          <div id="events_tab_ready" class="events">
            <%if params[:tab]== 'events'%>
              <% default_sort_by = profile_subject_is_current? ? "updated_at" : "popularity"%>
              <%= render partial: 'events/profile_events_list', :locals => {:scope => :me, :limit => 0, :page=> params[:page] || 1, :sort_by=> params[:sort_by] || default_sort_by }%>
            <%end%>
          </div>

          <div class="loader_pagination" style="display:none">
            <div class="pageless-loader" style="text-align:center;width:100%;">
              <div class="msg" style="font-size:2em">
                <%=t('search.loading')%>
              </div>
              <%= render :partial => "layouts/loading" %>
            </div>
          </div>

        </div>
      <%end%>

      <!-- WORKSHOPS -->

      <% if availableModels.include? "Workshop" %>
        <div class="tab-pane <%=params[:tab]=='workshops' ? 'active':''%>" id="tabWorkshops">

          <%= render partial: 'users/order_by_dropdown_workshops' %>
          <div id="workshops_tab_ready" class="entities">
            <% if params[:tab] == 'workshops'%>
              <% default_sort_by = profile_subject_is_current? ? "updated_at" : "popularity"%>
              <%= render partial: 'workshops/profile_workshops_list', :locals => {:scope => :me, :limit => 0, :page=> params[:page] || 1, :sort_by=> params[:sort_by]||default_sort_by}%>
            <%end%>
          </div>

          <div class="loader_pagination" style="display:none">
            <div class="pageless-loader" style="text-align:center;width:100%;">
              <div class="msg" style="font-size:2em">
                <%=t('search.loading')%>
              </div>
              <%= render :partial => "layouts/loading" %>
            </div>
          </div>

        </div>
      <%end%>


      <!-- ************************ -->
      <!-- following / followers -->
      <!-- ************************ -->

      <div class="tab-pane" id="tab5">
        <ul id="followers_ul" class="items">

        </ul>
        <div class="loader_pagination" style="display:none">
          <div class="pageless-loader" style="text-align:center;width:100%;">
            <div class="msg" style="font-size:2em">
              <%=t('search.loading')%>
            </div>
            <%= render :partial => "layouts/loading" %>
          </div>
        </div>
      </div>

      <div class="tab-pane" id="tab6">
        <ul id="followings_ul" class="items">

        </ul>

        <div class="loader_pagination" style="display:none">
          <div class="pageless-loader" style="text-align:center;width:100%;">
            <div class="msg" style="font-size:2em">
              <%=t('search.loading')%>
            </div>
            <%= render :partial => "layouts/loading" %>
          </div>
        </div>
      </div>

    </div>
</section>

<%content_for :javascript do%>
    <% if params[:tab]=="excursions" %>
        excursions_tab_loaded = true;
        re_apply_pageless("excursions");
    <% elsif params[:tab]=="workshops" %>
        workshops_tab_loaded = true;
        re_apply_pageless("workshops");
    <% elsif params[:tab]=="events" %>
        events_tab_loaded = true;
        re_apply_pageless("events");
    <% elsif params[:tab] == "categories" %>
        categories_tab_loaded = true;
        re_apply_pageless("categories");
    <% elsif params[:tab] == "resources" %>
        resources_tab_loaded = true;
        re_apply_pageless("resources");
    <% elsif params[:tab] == "followings" %>
        $("#followings_tab").trigger("click");
    <% elsif params[:tab] == "followers" %>
        $("#followers_tab").trigger("click");
    <%end%>
<%end%>
